var httpConf = require("../../../http/httpModule.js")
var util = require("../../../util/util.js")
var async = require("async")
module.exports = function(app) {
	return new DBRemote(app);
};

var DBRemote = function(app) {
	this.app = app
	DBRemote.app = app
    DBRemote.dbService = this.app.get("dbService")
    DBRemote.channelService = this.app.get('channelService')
    if(DBRemote.dbService && DBRemote.dbService.db){
    	DBRemote.db = DBRemote.dbService.db
    }
}

var local = {}

//------------------账号相关----------------------------//

//检查账号是否存在
DBRemote.prototype.check = function(result,msg,cb) {
	DBRemote.dbService.checkAccount(result,msg,cb)
}

//根据uid获取玩家信息
DBRemote.prototype.getPlayerInfoByUid = function(uid,cb) {
	DBRemote.dbService.getPlayerInfoByUid(uid,cb)
}
//根据unionid获取玩家信息
DBRemote.prototype.getPlayerInfo = function(unionid,cb) {
	DBRemote.dbService.getPlayerInfo(unionid,cb)
}
DBRemote.prototype.getValue = function(uid,name,cb) {
	DBRemote.dbService.getPlayer(uid,name,cb)
}
DBRemote.prototype.setValue = function(uid,name,value,cb) {
	DBRemote.dbService.setPlayer(uid,name,value,cb)
}
DBRemote.prototype.getPlayerNickName = function(uid,cb) {
	DBRemote.dbService.getPlayerString(uid,"nickname",function(data){
		cb(data)
	})
}
DBRemote.prototype.getPlayerObject = function(uid,name,cb) {
	DBRemote.dbService.getPlayerObject(uid,name,cb)
}
DBRemote.prototype.setPlayerObject = function(uid,name,value,cb) {
	DBRemote.dbService.setPlayerObject(uid,name,value,cb)
}
//设置token
DBRemote.prototype.setLoginToken = function(unionid,token,cb) {
	DBRemote.dbService.db.hset("loginToken",unionid,token)
	cb(true)
}
//token登陆检查
DBRemote.prototype.checkLoginToken = function(unionid,token,cb) {
	DBRemote.dbService.db.hget("loginToken",unionid,function(err,data) {
		if(err || !data){
			console.log(err)
			console.log(data)
			cb(false)
		}else{
			if(token == data){
				DBRemote.dbService.getUidByUnionid(unionid,function(flag,data) {
					if(flag){
						cb(true)
					}else{
						cb(false)
					}
				})
			}else{
				cb(false)
			}
		}
	})
}
//手机登录检查
DBRemote.prototype.checkLoginPhone = function(phone,password,cb) {
	DBRemote.dbService.db.hget("game:phoneAccount",phone,function(err,data) {
		if(err || !data){
			console.log(err)
			console.log(data)
			cb(false)
		}else{
			if(password == data){
				DBRemote.dbService.getUidByUnionid(phone,function(flag,data) {
					if(flag){
						cb(true)
					}else{
						cb(false)
					}
				})
			}else{
				cb(false)
			}
		}
	})
}
//获取当前lastid
DBRemote.prototype.getPlayerId = function(cb) {
	DBRemote.dbService.db.get("nn:acc:lastid",function(err,data) {
		cb(data)
	})
}

//------------------玩家属性相关----------------------------//

//设置属性
// DBRemote.prototype.setValue = function(uid,name,value,cb) {
// 	if(!value || typeof(value) != "number"){
// 		cb(false)
// 		return
// 	}
// 	switch(name){
// 		case "diamond":
// 			DBRemote.dbService.incrbyPlayer(uid,name,value,function(flag,curValue) {
// 				if(flag){
// 					//通知钻石更新
// 					var notify = {
// 						"cmd" : "updateDiamond",
// 						"data" : value,
// 						"curValue" : curValue
// 					}
// 					DBRemote.app.rpc.game.remote.sendByUid(null,uid,notify,function(){})
// 				}
// 				cb(flag)
// 			})
// 		return
// 		case "gold":
// 			DBRemote.dbService.incrbyPlayer(uid,name,value,function(flag,curValue) {
// 				if(flag){
// 					//通知钻石更新
// 					var notify = {
// 						"cmd" : "updateGold",
// 						"data" : value,
// 						"curValue" : curValue
// 					}
// 					DBRemote.app.rpc.game.remote.sendByUid(null,uid,notify,function(){})
// 				}
// 				cb(flag)
// 			})
// 		return
// 		default:
// 			cb(false)
// 	}
// }
//------------------物品相关----------------------------//
DBRemote.prototype.addItem = function(uid,itemId,count,cb) {
	if(!count || typeof(count) != "number"){
		cb(false)
		return
	}
	DBRemote.dbService.addItem(uid,itemId,count,cb)
}
DBRemote.prototype.addItemReason = function(uid,itemId,count,reason,cb) {
	if(!count || typeof(count) != "number"){
		cb(false)
		return
	}
	DBRemote.dbService.addItemReason(uid,itemId,count,reason,cb)
}
//------------------公告相关----------------------------//

//获取公告
DBRemote.prototype.getNotify = function(cb) {
	DBRemote.dbService.getNotify(cb)
}
//更新公告
DBRemote.prototype.updateNotify = function(notify,source,cb) {
	DBRemote.dbService.getNotify(function(data) {
		console.log(data)
		if(!data[source]){
			data[source] = {}
			data[source].name = ""
		}
		data[source].content = notify
		DBRemote.dbService.setNotify(data)
		//更新公告完通知所有玩家
		DBRemote.channelService.broadcast("connector","onNotify",{"type":"notify","data":data})
		if(cb){
			cb(true)
		}
	})
}


//------------------手机登陆相关----------------------------//

//设置手机绑定
DBRemote.prototype.setPhoneInfo = function(uid,phone,cb) {
	DBRemote.dbService.db.hget("uidPhoneMap",uid,function(data) {
		DBRemote.dbService.db.hdel("phoneUidMap",data)
		DBRemote.dbService.db.hset("phoneUidMap",phone,uid)
		DBRemote.dbService.db.hset("uidPhoneMap",uid,phone)
		cb(true)	
	})
}
//获取手机绑定信息
DBRemote.prototype.getPhoneInfo = function(uid,cb) {
	DBRemote.dbService.db.hget("uidPhoneMap",uid,function(err,data) {
		if(err || !data){
			cb(false)
		}else{
			cb(data)
		}
	})
}
//获取手机号对应uid
DBRemote.prototype.getUidByPhoneInfo = function(phone,cb) {
	DBRemote.dbService.db.hget("phoneUidMap",phone,function(err,data) {
		if(err || !data){
			cb(false)
		}else{
			cb(data)
		}
	})
}
//手机登陆绑定token
DBRemote.prototype.bindToken = function(phone,cb) {
	var uid
	var unionid
	var loginToken
	async.waterfall([
		function(next) {
			//获取uid
			DBRemote.dbService.db.hget("phoneUidMap",phone,function(err,data) {
				if(err || !data){
					console.log("手机号不存在")
					cb(false)
				}else{
					uid = data
					next()
				}
			})
		},
		function(next) {
			//获取uidMap
			DBRemote.dbService.getPlayerString(uid,"uidMap",function(data) {
				if(!data){
					console.log("获取uidMap error : "+data)
					cb(false)
				}else{
					unionid = data
					next()
				}
			})
		},
		function() {
			var loginToken = util.randomString()
			DBRemote.dbService.db.hset("loginToken",unionid,loginToken,function(err,data) {
				cb({unionid : unionid,loginToken : loginToken})
			})
		}
		],function(err) {
		console.log(err)
	})
}
//创建账号
DBRemote.prototype.createAccount = function(result,cb) {
	DBRemote.dbService.createAccount(result,cb)
}
//修改昵称
DBRemote.prototype.changeNickname = function(uid,nickname,cb) {
	DBRemote.dbService.setPlayer(uid,"nickname",nickname,cb)
}
//修改头像
DBRemote.prototype.changeHeadType = function(uid,head,cb) {
	DBRemote.dbService.setPlayer(uid,"head",head,cb)
}
//修改密码
DBRemote.prototype.changePassword = function(phone,password,cb) {
	DBRemote.dbService.db.hget("game:phoneAccount",phone,function(err,ps) {
		console.log("ps : ",ps)
		if(err || !ps){
			cb(false,"账号不存在")
			return
		}
		if(password == ps){
			cb(false,"不能和之前的密码一样")
			return
		}
		DBRemote.dbService.db.hset("game:phoneAccount",phone,password,function() {
			cb(true)
			return
		})
	})
}